package com.cskaoyan.servlet;

import com.cskaoyan.bean.MarketIssue;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.AdminIssueService;
import com.cskaoyan.service.AdminIssueServiceImpl;
import com.cskaoyan.util.JacksonUtil;
import com.cskaoyan.util.ResponseUtil;
import lombok.SneakyThrows;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author bianyonglei
 * @since 2023/05/30 22:38
 */
@WebServlet("/admin/issue/*")
public class AdminIssueServlet extends WdBaseServlet {
    private AdminIssueService adminIssueService = new AdminIssueServiceImpl();
    @SneakyThrows
    private void list(HttpServletRequest request, HttpServletResponse response){
        Integer page = Integer.parseInt(request.getParameter("page"));
        Integer limit = Integer.parseInt(request.getParameter("limit"));
        String sort = request.getParameter("sort");
        String order = request.getParameter("order");
        String question = request.getParameter("question");
        List<MarketIssue> marketIssues= adminIssueService.list(question,page,limit,sort,order);
        ResponseUtil.responseOkJsonVo(response, CommonData.data(marketIssues));
    }
    @SneakyThrows
    private void create(HttpServletRequest request, HttpServletResponse response){


        String requestBody = request.getReader().readLine();
        MarketIssue marketIssue = JacksonUtil.read(requestBody, MarketIssue.class);
        int row = adminIssueService.create(marketIssue);

        if(row == 200){
            ResponseUtil.responseOkJsonVo(response, marketIssue);
        }else {
            ResponseUtil.responseJson(response, BaseRespVo.fail("创建常见问题失败"));
        }


    }
    @SneakyThrows
    public void update(HttpServletRequest request, HttpServletResponse response){
        String requestBody = request.getReader().readLine();
        MarketIssue marketIssue = JacksonUtil.read(requestBody, MarketIssue.class);
        int row = adminIssueService.update(marketIssue);
        if(row == 200){
            ResponseUtil.responseOkJsonVo(response, marketIssue);
        }else{
            ResponseUtil.responseJson(response, BaseRespVo.fail("该通用问题更新失败！"));
        }
    }

    @SneakyThrows
    public BaseRespVo delete(HttpServletRequest request, HttpServletResponse response){
        String requestBody = request.getReader().readLine();
        MarketIssue marketIssue = JacksonUtil.read(requestBody, MarketIssue.class);

        System.out.println(marketIssue);
        int row = adminIssueService.delete(marketIssue);
        if(row == 200){
            return BaseRespVo.ok();
        }else{
            return BaseRespVo.fail("删除失败");
        }
       // return null;
    }

}
